<template>
  <div class="login-phone-container">
    <van-nav-bar 
      title="手机号登录" 
      left-text="返回"
      left-arrow
      @click-left="router.back()"
    />
    
    <div class="header">
      <h2>手机号登录</h2>
      <p>使用手机号快捷登录</p>
    </div>
    
    <div class="form-container">
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="phone"
            type="tel"
            name="phone"
            placeholder="请输入手机号"
            :rules="[
              { required: true, message: '请输入手机号' },
              { pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误' }
            ]"
          >
            <template #left-icon>
              <van-icon name="phone-o" />
            </template>
            <template #button>
              <van-button size="small" type="primary" @click="sendCode" :disabled="counting > 0">
                {{ counting > 0 ? `${counting}s后重试` : '发送验证码' }}
              </van-button>
            </template>
          </van-field>

          <van-field
            v-model="sms"
            center
            name="sms"
            placeholder="请输入短信验证码"
            :rules="[{ required: true, message: '请输入短信验证码' }]"
          >
            <template #left-icon>
              <van-icon name="shield-o" />
            </template>
          </van-field>
        </van-cell-group>

        <div class="submit-btn">
          <van-button round block type="primary" native-type="submit" size="large">
            登录
          </van-button>
        </div>
      </van-form>

      <div class="helper-links">
        <router-link to="/register" class="register-link">注册账号</router-link>
        <router-link to="/login" class="password-login">密码登录</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { showSuccessToast, showToast } from 'vant'

const router = useRouter()
const phone = ref('')
const sms = ref('')
const counting = ref(0)

const sendCode = () => {
  if (!/^1[3-9]\d{9}$/.test(phone.value)) {
    showToast('请输入正确的手机号')
    return
  }
  
  if (counting.value > 0) return
  
  counting.value = 60
  const timer = setInterval(() => {
    counting.value--
    if (counting.value <= 0) {
      clearInterval(timer)
    }
  }, 1000)
  
  showSuccessToast('验证码已发送')
}

const onSubmit = (values) => {
  console.log('submit', values)
  showSuccessToast('登录成功')
  router.push('/')
}
</script>

<style lang="scss" scoped>
.login-phone-container {
  min-height: 100vh;
  background: #f7f8fa;
  padding-bottom: 40px;

  .header {
    background: linear-gradient(135deg, #1989fa, #39b9f9);
    padding: 48px 24px 64px;
    color: white;
    text-align: center;
    border-radius: 0 0 24px 24px;

    h2 {
      font-size: 28px;
      margin: 0 0 12px;
      font-weight: 600;
    }

    p {
      font-size: 16px;
      margin: 0;
      opacity: 0.9;
    }
  }

  .form-container {
    margin-top: -32px;
    padding: 0 16px;

    :deep(.van-cell-group--inset) {
      margin: 0;
      border-radius: 12px;
      box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
    }

    :deep(.van-field__left-icon) {
      margin-right: 8px;
      color: #666;
    }
  }

  .submit-btn {
    margin-top: 24px;
    padding: 0 16px;
  }

  .helper-links {
    display: flex;
    justify-content: space-between;
    margin-top: 16px;
    padding: 0 16px;

    a {
      color: #1989fa;
      text-decoration: none;
      font-size: 14px;
    }
  }
}
</style>
